package com.anro.util

import android.content.pm.ActivityInfo
import android.os.Bundle
import android.view.LayoutInflater
import androidx.appcompat.app.AppCompatActivity
import androidx.viewbinding.ViewBinding
import com.gyf.immersionbar.ktx.immersionBar
import java.lang.reflect.ParameterizedType

/**
 * Kotlin Activity 基类
 * 实现 ViewBinding
 * 集成的项目需要在build.gradle中开启ViewBinding
 * 具体操作
 * android {
 *   ...
 *   buildFeatures {
 *      viewBinding true
 *   }
 * }
 *
 */
abstract class  BaseKtActivity<T : ViewBinding> : AppCompatActivity(){
    lateinit var binding: T

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val superclass = javaClass.genericSuperclass
        val aClass = (superclass as ParameterizedType).actualTypeArguments[0] as Class<*>
        val method = aClass.getDeclaredMethod("inflate", LayoutInflater::class.java)
        binding = method.invoke(null, layoutInflater) as T
        setContentView(binding.root)

        ActivityCollector.instance.addActivity(this)
        //保持竖屏
        requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT

        initView()
        initData()
    }


    abstract fun initView()

    abstract fun initData()

    override fun onDestroy() {
        super.onDestroy()
        ActivityCollector.instance.removeActivity(this)
    }

}